<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="inspectionName">
      <el-input v-model="dataForm.inspectionName" placeholder="项目名称"></el-input>
    </el-form-item>
    <el-form-item label="项目类别" prop="className">
            <el-select v-model="dataForm.className" placeholder="项目类型">
        <el-option label="拍片类" value="拍片类"></el-option>
        <el-option label="化验类" value="化验类"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="项目单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="项目单价"></el-input>
    </el-form-item>
    <el-form-item label="项目成本" prop="cost">
      <el-input v-model="dataForm.cost" placeholder="项目成本"></el-input>
    </el-form-item>

    <el-form-item label="单位" prop="unit">
      <el-input v-model="dataForm.unit" placeholder="单位"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {InspectService} from '../../model/zyj/InspectService.js'
  const inspectService = InspectService.getInstance()
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          insId: 0,
          mid: '',
          hid: '',
          inspectionName: '',
          className: '',
          unitPrice: '',
          cost: '',
          createTime: '',
          updateTime: '',
          unit: '',
          classId: ''
        },
        dataRule: {
          mid: [
            { required: true, message: '添加用户id不能为空', trigger: 'blur' }
          ],
          hid: [
            { required: true, message: '医院id不能为空', trigger: 'blur' }
          ],
          inspectionName: [
            { required: true, message: '项目名称不能为空', trigger: 'blur' }
          ],
          class: [
            { required: true, message: '项目类别不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '项目单价不能为空', trigger: 'blur' }
          ],
          cost: [
            { required: true, message: '项目成本不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          classId: [
            { required: true, message: '类别id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.insId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.insId) {
            inspectService.queryById(id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.hisInspectionfee
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            inspectService.saveOrUpdate(this.dataForm).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }
        })
      }
    }
  }
</script>
